import { defineStore } from 'pinia'
import Api from '@/api'
import Dict from '@/Utils/dict'
import { checkRule } from '@/Utils/finance'
import { sessionGet } from '@/Utils/storage'
export const useFinanceStore = defineStore('finance', {
  persist: {
    enabled: true,
  },
  state: () => {
    return {
      // financeAAA: '',
      // financeBBB: '',
      // // 版本
      // version: {},
      getProfitYoy: {
        title: '',
        titleRate: '',
        titleMonth: '',
        titleMount: '',
        sub_titles: [],
        info: [],
      },
      getProfitMom: {
        title: '',
        titleRate: '',
        titleMonth: '',
        titleMount: '',

        sub_titles: [],
        info: [],
      },
      getProfitTrendVolume: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getProfitTrendRate: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getProfitQoq: {
        info: [],
        title: '',
        titleM: '',
        sub_titles: [],
      },
      getIncomeYOY: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getIncomeQOQ: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getTopFiveCountryRankingZero: {
        tableHeadData: [],
        tableBodyData: [],
        title: '',
      },
      getTopFiveCountryRankingOne: {
        tableHeadData: [],
        tableBodyData: [],
        title: '',
      },
      getAchievementRate: {
        info: [],
        title: '',
        sub_titles: [],
        profitAmount: 0,
        topTitle: '',
      },
      getIncomeVolumeRate: {
        info: [],
        title: '',
        sub_titles: [],
      },
      /****边际贡献****/
      getMarginalAmountRateTrend: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getMarginalRateTrend: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getMarginalAmountYOY: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getMarginalAmountMOMGrow: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getMarginalAmoutRate: {
        title: '',
        info: {
          money: '',
          moneyNubem: '',
          units: '',
          profit: '',
          amountRatio: '',
        },
      },
      getMarginalAmountYOYMOMCompare: {
        info: [],
        title: '',
        sub_titles: [],
      },
      /****变动支出 谷守丙****/
      getVariableExpensesCost: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getVariableExpensesWages: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getVariableExpensesAgencyfee: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getVariableExpensesTax: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getVariableExpensesPromotion: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getEveryVariableExpensesCost: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getEveryVariableExpensesWages: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getEveryVariableExpensesAgencyfee: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getEveryVariableExpensesTax: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getEveryVariableExpensesPromotion: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getKeyVariableExpensesMonth: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getVariableExpensesByRegionMonth: {
        info: [],
        title: '',
        sub_titles: [],
      },
      /***收入****/
      getSalesAmountProfit: {
        title: '',
        info: {
          money: '',
          moneyNubem: '',
          units: '',
          profit: '',
          amountRatio: '',
        },
      },
      getSalesAmountYOY: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getSalesAmountMOM: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getSalesPercent: {
        info: [],
        title: '',
      },
      getSalesTrend: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getRevenueGrowthRate: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getSalesCompare: {
        info: {
          money: '',
          moneyNubem: '',
          amountRatio: '',
          profit: '',
          units: ''

        },
        title: ""
      },
      /***固定支出总额，增加额 张绅绅****/
      getFixedExpensesAmount: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getFixedExpensesAmountYOY: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getFixedExpensesAmountYoyGrow: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getFixedExpensesAmountQoq: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getFixedExpensesAmountYoyQoq: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getTsotalSumAndRatio: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getFixedExpenses: {
        info: [],
        title: '',
        sub_titles: [],
      },
      /********全球财务接口-固定支出人工、咨询、公关、差旅，租赁 朱国清************/
      getExpensesLaborCost: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getExpensesLeaseCost: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getExpensesConsultingCost: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getExpensesPublicRelationsCost: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getExpensesTrafficCost: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getContrastExpensesLaborCost: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getContrastExpensesLeaseCost: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getContrastExpensesConsultingCost: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getContrastExpensesPublicRelationsCost: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getContrastExpensesTrafficCost: {
        info: [],
        title: '',
        sub_titles: [],
      },
      getGlobalVariableExpensesCumulativeYear:{
        info: [],
        title: '',
        sub_titles: [],
      }
    }
  },

  actions: {
    clearVersion() {
      this.version = {}
    },
    getAll() {
      const financeLevel = sessionGet('role')?.financeLevel || 0
      if (!financeLevel) {
        return ''
      }
      Object.keys(Api.financeApi).forEach((item: any) => {
        // console.log('getAll', item)
        try {
          // 根据字典里规则校验是否调此接口
          checkRule(item) &&
            Api.financeApi[item]().then((res: any) => {
              if (res.code == 0 || res.code == 10000) {
                if (res.data) {
                  this[item] = res.data
                }
              }
            })
        } catch (e) {
          console.log('err', e)
        }
      })
      // // Dict对象获取finance的所有key,然后放到一个数组中
      // const keys = Object.keys(Dict.finance).filter((item: any) => {
      //   if (!item) {
      //     return false
      //   }
      //   return true
      // })

      // if (keys.length) {
      //   keys.forEach((item: any) => {
      //     try {
      //       Api[item]().then((res: any) => {
      //         if (res.code == 0 || res.code == 10000) {
      //           if (res.data) {
      //             this[item] = res.data
      //           }
      //         }
      //       })
      //     } catch (e) {
      //       console.log('err', e)
      //     }
      //   })
      // }
    },
  },
})
